<template>
  <section class="table-wrap">
    <div class="action-wrap">
      <el-form ref="paramRef" :model="params" inline>
        <el-form-item label="用户名：" prop="loginName">
          <el-input
            v-model="params.loginName"
            placeholder="请输入用户名"
            clearable
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableList" style="width: 100%">
      <el-table-column prop="loginName" label="登录名" align="center" />
      <el-table-column prop="name" label="昵称" align="center" />
      <el-table-column prop="password" label="密码" align="center"> </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="warning" link @click="handleEdit(scope.row)">修改</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row.id)">删除</el-button>
          <el-button type="info" link @click="handleResetPassword(scope.row)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination ref="pageRef" :total="total" @query="queryHandler" />
    <Dialog
      :visible="visible"
      title="用户表单"
      :width="700"
      @cancel="handleCancel"
      @confirm="handleConfirm"
    >
      <el-form :model="form" label-width="120" style="max-width: 600px">
        <el-form-item label="登录名">
          <el-input
            v-model="form.loginName"
            placeholder="请输入"
            :maxlength="10"
            :disabled="!!form.id"
          ></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.name" :maxlength="10" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="密码" v-show="!form.id">
          <el-input v-model="form.password" :maxlength="32" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
    </Dialog>
    <Dialog
      :visible="resetVisible"
      title="重置密码"
      :width="700"
      @cancel="handleResetCancel"
      @confirm="handleResetConfirm"
    >
      <el-form :model="form" label-width="120" style="max-width: 600px">
        <el-form-item label="新密码">
          <el-input v-model="form.password" type="password" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
    </Dialog>
  </section>
</template>

<script setup>
import Pagination from '@/components/pagination/Pagination.vue'
import { getUserList, saveUser, deleteUser, resetPassword } from '../../api/user'
import Dialog from '@/components/dialog/Dialog.vue'
import crypto from '@/utils/crypto.js'

const pageRef = ref()
const total = ref(0)
const tableList = ref([])
const visible = ref(false)
const form = ref({})
const paramRef = ref()
const params = ref({
  loginName: ''
})
const resetVisible = ref(false)

const handleResetCancel = () => {
  resetVisible.value = false
}

const handleResetConfirm = () => {
  handleResetSPassword()
}

const handleResetPassword = (row) => {
  form.value = JSON.parse(JSON.stringify(row))
  form.value.password = null
  resetVisible.value = true
}

const handleCancel = () => {
  visible.value = false
}

const handleConfirm = () => {
  handleSave()
}

const handleAdd = () => {
  form.value = {}
  visible.value = true
}

const handleEdit = (row) => {
  form.value = JSON.parse(JSON.stringify(row))
  visible.value = true
}

const handleDelete = (id) => {
  ElMessageBox.confirm('确认删除该条数据？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const { success, message } = await deleteUser(id)
      if (success) {
        pageRef.value.query()
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
      } else {
        ElMessage({
          type: 'error',
          message
        })
      }
    })
    .catch(() => {})
}

const handleSave = async () => {
  const { success, message } = await saveUser({
    ...form.value,
    password: crypto.md5Hex(form.value.password)
  })
  if (success) {
    handleCancel()
    pageRef.value.query()
    ElMessage({
      type: 'success',
      message: '保存成功'
    })
  } else {
    ElMessage({
      type: 'error',
      message
    })
  }
}

const handleResetSPassword = async () => {
  const { success, message } = await resetPassword({
    ...form.value,
    password: crypto.md5Hex(form.value.password)
  })
  if (success) {
    handleResetCancel()
    pageRef.value.query()
    ElMessage({
      type: 'success',
      message: '保存成功'
    })
  } else {
    ElMessage({
      type: 'error',
      message
    })
  }
}

const getList = async (current, pageSize) => {
  const { success, data, message } = await getUserList({
    loginName: params.value.loginName,
    page: current,
    size: pageSize
  })

  if (success) {
    tableList.value = data.list
    total.value = data.total
  } else {
    ElMessage({
      type: 'error',
      message
    })
  }
}

const handleReset = () => {
  paramRef.value.resetFields()
  pageRef.value.resetQuery()
}

const handleQuery = () => {
  pageRef.value.resetQuery()
}

const queryHandler = (current, pageSize) => {
  getList(current, pageSize)
}
</script>

<style lang="scss" scoped></style>
../../api/user
